<?php

defined('SYSPATH') or die('Access error!');

/**
 * 用户的基本信息类
 * ============================================================================
 * 功能描述： 
 *       1:基本资料
 *       2:修改密码
 *       3:修改联系方式
 *       4:处理头像
 *       5:修改用户中心模板 
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_User_Base extends Controller_User_Main
{

    private $user;

    public function before()
    {
        parent::before();
        $this->user = Model::factory('user');
    }

    public function action_index()
    {
        //当前位置
        $this->set_define_position();
        $this->template->view('user_index');
    }

    //修改用户的基本资料
    public function action_profile()
    {
        //当前位置
        $this->set_define_position(__('user base info'));
        //根据用户所在地
        $area = $this->get_area_cache_file();
        $this->template->assign('province', $area['province']);
        $this->template->assign('city', $area['city']);
        
        //提交修改
        if ($_POST)
        {
            $data = array();
            $data['nickname'] = Arr::get($_POST, 'nickname', '');
            $data['realname'] = Arr::get($_POST, 'realname', '');
            $data['province'] = Arr::get($_POST, 'province', 0);
            $data['birthday'] = Arr::get($_POST, 'birthday', '0000-00-00');
            $data['description'] = HTML::get_safe_content(Arr::get($_POST, 'description', ''));
            $data['city'] = Arr::get($_POST, 'city', 0);
            $data['sex'] = Arr::get($_POST, 'sex', 1);
            $data['last_time'] = Date::get_time();
            $data['last_ip'] = URL::get_ip();
            $data['birthday'] = Date::get_mk_time($data['birthday']);

            if ($this->user->is_exist(array('nickname' => $data['nickname']), $this->user_detail['id']))
            {
                Message::error(__('nickname exist'));
                $this->request->redirect('user/base/profile');
            }

            if ($this->user->update($data, array('id' => $this->user_detail['id'])))
            {
                Message::success(__('user update success'));
                $this->request->redirect('user/base/profile');
            }
        }
        $this->template->assign('data', $this->user_detail);
        $this->template->view('user_profile');
    }

    //修改密码
    public function action_password()
    {
        //当前位置
        $this->set_define_position(__('change password'));
        
        if ($_POST)
        {
            $oldpassword = Arr::get($_POST, 'oldpassword', '');
            $password = Arr::get($_POST, 'password', '');
            $confim_password = Arr::get($_POST, 'confim_password', '');
            //检查用户的老密码
            $login_flag = $this->user->check_user($this->user_detail['username'], $oldpassword);
            if ($login_flag === FALSE || strlen($oldpassword) < 6 || strlen($oldpassword) > 20)
            {
                Message::error(Message::ERROR, __('old password error'));
                $this->request->redirect('user/base/password');
            }

            if (strlen($password) < 6 || strlen($password) > 20)
            {
                Message::error(__('new password error'));
                $this->request->redirect('user/base/password');
            }

            if ($password != $confim_password)
            {
                Message::error(__('new password no equal confim_password'));
                $this->request->redirect(Route::get('userinfo')->uri(array('action' => 'password')));
            }
            $data = array();
            $data['password'] = $password;
            $data['safecode'] = $this->user_detail['safecode'];
            $data['id'] = $this->user_detail['id'];
            if ($this->user->change_user_password($data))
            {
                Message::success(__('change password success'));
                $this->request->redirect('user/base/password');
            }
        }
        $this->template->view('user_change_password');
    }

    //处理联系方式
    public function action_contact()
    {
        //当前位置
        $this->set_define_position(__('change contact us'));
        
        if ($_POST)
        {
            $data = array();
            $data['qq'] = Arr::get($_POST, 'qq', '');
            $data['msn'] = Arr::get($_POST, 'msn', '');
            $data['alias'] = Arr::get($_POST, 'alias', '');
            $data['mobile_phone'] = Arr::get($_POST, 'mobile_phone', '');
            $data['office_phone'] = Arr::get($_POST, 'office_phone', '');
            $data['home_phone'] = Arr::get($_POST, 'home_phone', '');
            if (!$this->user->validate($data))
            {
                Message::error( __('validate error'));
                $this->request->redirect('user/base/contact');
            }

            if ($this->user->update($data, array('id' => $this->user_detail['id'])))
            {
                Message::success( __('contact update success'));
                $this->request->redirect('user/base/contact');
            }
        }
        $this->template->assign('data', $this->user_detail);
        $this->template->view('user_contact');
    }

    //处理头像 
    public function action_avatar()
    {
        //当前位置
        $this->set_define_position(__('change avatar'));
        //获取图片
        $avatar_path ='static/user/images/avatar/'.$this->user_detail['id'] . '/' . $this->user_detail['avatar'];
        if(count($_POST)>0){
             $image_info  = getimagesize($avatar_path);
             $width = intval($_POST['w']) < 30 ?$image_info[0]:$_POST['w'];
             $height =intval($_POST['h']) < 30 ?$image_info[1]:$_POST['h'];
             $x = isset($_POST['x'])&&intval($_POST['w'])!=0?$_POST['x']:0;
             $y = isset($_POST['y'])&&intval($_POST['w'])!=0?$_POST['y']:0;
             $images = Image::factory($avatar_path);
             $images->crop($width, $height, $x,$y);
             $images->resize(100,100);
             $images->save(USER_AVATAR_DIR . $this->user_detail['id'] . '/' . 'small_' . $this->user_detail['avatar']);
        }
        $this->template->assign('user_avatar', USER_AVATAR_PATH.'/'.$this->user_detail['id'] . '/' . $this->user_detail['avatar']);
        $this->template->assign('action_upload_avatar', URL::site('user/base/upload_avatar'));
        $this->template->assign('action_crop', URL::site('user/base/avatar'));
        $this->template->view('user_avatar');
    }

    //上传头像
    public function action_upload_avatar()
    {
        $data = array();
        $dir = USER_AVATAR_DIR . $this->user_detail['id'] . '/';
        $uploads = Upload::instance($dir)->upload('avatar', 'png|jpg|gif', $this->user_detail['id'].'_avatar');
        if (count($uploads) > 0 && $uploads !== FALSE)
        {
            $data['avatar'] = $uploads[0]['file_realname'];
            if ($this->user->update($data, array('id' => $this->user_detail['id'])))
            {
                Message::success('上传成功');
                $this->request->redirect('user/base/avatar');
            }
        }
        Message::error('上传失败');
        $this->request->redirect('user/base/avatar');
    }

    //修改用户的风格
    public function action_theme()
    {
        $this->set_define_position(__('change style'));
        //获取用户的风格
        $user_theme = $this->user->get_var(array('id' => $this->user_detail['id']), 'theme');
        $this->template->assign('current_theme', $user_theme);
        /* 获得可用的模版 */
        $user_themes = array();
        $template_dir = @opendir(USER_TEMPLATE_STYLE);

        while ($file = readdir($template_dir))
        {
            if ($file != '.' && $file != '..' && is_dir(USER_TEMPLATE_STYLE . $file) && $file != '.svn')
            {
                $user_themes[] = $this->user->get_themes_info($file);
            }
        }
        @closedir($template_dir);
        $this->template->assign('user_themes', $user_themes);
        $this->template->view('user_themes');
    }

    public function action_save_theme()
    {
        $data = array();
        $data['theme'] = Arr::get($_POST, 'theme', 'green');
        if ($this->user->update($data, array('id' => $this->user_detail['id'])))
        {
            Session::instance()->set_detail('user', 'theme', $data['theme']);
            Message::success(__('update theme success'));
            $this->request->redirect('user/base/theme');
        }
    }

}